package com.cloudz.iptv.fragment.content.v1.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.cloudz.iptv.App;
import com.cloudz.iptv.R;
import com.cloudz.iptv.fragment.content.v1.adapter.data.ItemImageTextBean;
import com.owen.adapter.CommonRecyclerViewAdapter;
import com.owen.adapter.CommonRecyclerViewHolder;
import com.owen.adapter.CommonViewHolder;
import com.owen.tvrecyclerview.widget.SpannableGridLayoutManager;

public class SpannableGridAdapter extends CommonRecyclerViewAdapter
{
    private RecyclerView recyclerView;

    public SpannableGridAdapter(Context context, RecyclerView recyclerView)
    {
        super(context);
        this.recyclerView = recyclerView;
    }

    @Override
    public int getItemLayoutId(int viewType)
    {
        return R.layout.item_grid_text_image_layout;
    }

    @Override
    public void onBindItemHolder(CommonRecyclerViewHolder helper, Object item, int position)
    {
        ItemImageTextBean itemBean = (ItemImageTextBean) item;

        CommonViewHolder holder = helper.getHolder();
        holder.setText(R.id.title, itemBean.title);

        ImageView imageView = helper.getHolder().getView(R.id.image);

        Glide.with(App.get()).load(itemBean.imgUrl).placeholder(R.drawable.placeholder).into(imageView);

        // 合并单元格
        final View itemView = helper.itemView;

        final boolean isVertical = recyclerView.getLayoutManager().canScrollVertically();

        // 获得布局
        final SpannableGridLayoutManager.LayoutParams lp = (SpannableGridLayoutManager.LayoutParams) itemView.getLayoutParams();


//        final int span1 = (position == 0 || position == 6 || position == 13 || position == 5 ? 2 : 1);
//        final int span2 = (position == 0 || position == 6 || position == 13 ? 2 : position == 5 ? 4 : 1);

        // 位置所占比例 position == 0， 占横行2个单位
        final int span1 = (position == 0  ? 2 : 1);
        final int span2 = (position == 0  ? 1 : 1);

        final int colSpan = (isVertical ? span2 : span1);
        final int rowSpan = (isVertical ? span1 : span2);
        if (lp.rowSpan != rowSpan || lp.colSpan != colSpan) {
            lp.rowSpan = rowSpan; //行
            lp.colSpan = colSpan; //列

            itemView.setLayoutParams(lp);
        }

    }
}
